//
// Created by 123 on 2024/12/26.
//
#include <stdio.h>
#include "stdlib.h"

int main060(void){
    int* ptr;
    int n1 = 5;
    int n2 = 10;

    //使用Malllock分配内存
    ptr =(int*)malloc(n1* sizeof(int ));
    if(ptr == NULL){
        printf("mem set failur\n");
        return 1;
    }
    //初始化数组
    for (int i = 0; i < n1; ++i) {
        ptr[i] = i+1;
    }
    //输出数组
    for (int i = 0; i < n1; ++i) {
        printf("%d ",ptr[i]);
    }
    printf("\n");
    ptr = (int*)realloc(ptr,n2* sizeof(int ));
    if(ptr == NULL){
        printf("mem reset failure\n");
        exit(1);
    }
    //打印下最新ptr
    for (int i = 0; i < n2; ++i) {
        printf("%d ",ptr[i]);
    }
    printf("\n");
    //再次初始化新增的元素
    for (int i = n1; i < n2; ++i) {
        ptr[i] = i+1;
    }
    //再次打印ptr
    for (int i = 0; i < n2; ++i) {
        printf("%d ",ptr[i]);
    }
    printf("\n");
    free(ptr);
    //free释放后，保留内存地址，内存数据被其他程序使用
    for (int i = 0; i < n2; ++i) {
        printf("%d ",ptr[i]);
    }
    //将指针设置为null
    ptr = NULL;
    //尝试安全访问ptr
    if(ptr != NULL){
        printf("ptr的值:%d\n",*ptr);
    }else{
        printf("ptr has been release");
    }

    return 0;
}